package com.wfm.leetcode.editor.cn;

import com.wfm.common.TreeNode;

/**
 * 对称二叉树
 * 2025-03-11 20:01:59
 * 时间复杂度 O(n)
 * 空间复杂度 O(n)
 */
class SymmetricTree {

//leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean isSymmetric(TreeNode root) {
        return check(root,root);
    }
    public boolean check(TreeNode p , TreeNode q){
        // 两棵树都是空，相同
        if(p==null&&q==null){
            return true;
        }
        // 一棵树空、一颗非空 不相同
        if(p==null||q==null){
            return false;
        }
        // 都不空，当前节点值相等，并且左=右，右=左
        return p.val==q.val && check(p.left,q.right)&&check(p.right,q.left);
    }
}
//leetcode submit region end(Prohibit modification and deletion)

    public static void main(String[] args) {
        Solution solution = new SymmetricTree().new Solution();
        
    }
}